package com.youshang.test;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.ssl.SSLContextBuilder;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import java.io.*;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Http工具类
 */
public class HttpClientUtil {

	private static final String DEFAULT_ENCODING = "UTF-8";

	private static final String SCHEMER_HTTP = "http://";

	private static final String SCHEMER_HTTPS = "https://";

	/** 默认超时时间 */
	private static final int DEFAULT_TIMEOUT = 60000;

	/**
	 * 发送post请求，默认发送字符编码和接受字符编码UTF-8,
	 *
	 * @param url
	 * @param map
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPost(String url,Map<String,String> map) throws ClientProtocolException, IOException{
		return sendPost(url,map,DEFAULT_ENCODING,DEFAULT_ENCODING);
	}

	/**
	 * 发送post请求，默认发送字符编码和接受字符编码UTF-8,
	 *
	 * @param url
	 * @param map
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPost(String url,Map<String,String> map,int timeout) throws ClientProtocolException, IOException{
		return sendPost(url,map,DEFAULT_ENCODING,DEFAULT_ENCODING,timeout);
	}

	/**
	 * 发送post请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @param readEncoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPost(String url,Map<String,String> map,String readEncoding) throws ClientProtocolException, IOException{
		return sendPost(url,map,readEncoding,DEFAULT_ENCODING);
	}

	/**
	 * 发送post请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @param readEncoding
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPost(String url,Map<String,String> map,String readEncoding,int timeout) throws ClientProtocolException, IOException{
		return sendPost(url,map,readEncoding,DEFAULT_ENCODING,timeout);
	}

	/**
	 * 发送post请求
	 *
	 * @param url
	 * @param map
	 * @param sendEncoding
	 * @param readEncoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPost(String url,Map<String,String> map,String sendEncoding,String readEncoding) throws ClientProtocolException, IOException{
		return sendPost(url,map,sendEncoding,readEncoding,DEFAULT_TIMEOUT);
	}

	/**
	 * 发送post请求
	 *
	 * @param url
	 * @param map
	 * @param sendEncoding
	 * @param readEncoding
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPost(String url,Map<String,String> map,String sendEncoding,String readEncoding,int timeout) throws ClientProtocolException, IOException{

		if(url.startsWith(SCHEMER_HTTPS)) return sendPostSSL(url,map,sendEncoding,readEncoding,timeout);

		HttpClient httpClient = HttpClients.createDefault();

		HttpPost post = null;

		String result = null;

		try{
			post = new HttpPost(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout)
					.setConnectTimeout(timeout).setSocketTimeout(timeout).build();
			post.setConfig(requestConfig);

			List<NameValuePair> formParams = new ArrayList<NameValuePair>();

			if(map != null)
			for(String key : map.keySet()){
				String value = map.get(key);
				formParams.add(new BasicNameValuePair(key, value));
			}

			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams,sendEncoding);
			post.setEntity(formEntity);

			HttpResponse response = httpClient.execute(post);

			InputStream in = response.getEntity().getContent();

			result = IoUtil.getStringFromInputSream(in, readEncoding);

			in.close();
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			if(post!=null && !post.isAborted()){
				post.abort();
			}
		}


		return result;
	}

	/**
	 *
	 *
	 * @param url
	 * @param map
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPostSSL(String url,Map<String,String> map) throws ClientProtocolException, IOException{
		return sendPostSSL(url,map,DEFAULT_ENCODING);
	}

	/**
	 *
	 *
	 * @param url
	 * @param map
	 * @param encoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPostSSL(String url,Map<String,String> map,String encoding) throws ClientProtocolException, IOException{
		return sendPostSSL(url,map,encoding,encoding);
	}

	/**
	 *
	 *
	 * @param url
	 * @param map
	 * @param encoding
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPostSSL(String url,Map<String,String> map,String encoding,int timeout) throws ClientProtocolException, IOException{
		return sendPostSSL(url,map,encoding,encoding,timeout);
	}

	/**
	 * 发送post请求
	 *
	 * @param url 请求链接
	 * @param map 请求参数
	 * @param sendEncoding 发送字符集
	 * @param readEncoding 响应字符集
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPostSSL(String url,Map<String,String> map,String sendEncoding,String readEncoding) throws ClientProtocolException, IOException{
		return sendPostSSL(url,map,sendEncoding,readEncoding,DEFAULT_TIMEOUT);
	}

	/**
	 * 发送post请求
	 *
	 * @param url 请求链接
	 * @param map 请求参数
	 * @param sendEncoding 发送字符集
	 * @param readEncoding 响应字符集
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPostSSL(String url,Map<String,String> map,String sendEncoding,String readEncoding,int timeout) throws ClientProtocolException, IOException{

		if(url.startsWith(SCHEMER_HTTP)) return sendPost(url,map,sendEncoding,readEncoding,timeout);

		HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).build();

		HttpPost post = null;

		String result = null;

		try{
			post = new HttpPost(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout)
					.setConnectTimeout(timeout).setSocketTimeout(timeout).build();
			post.setConfig(requestConfig);

			List<NameValuePair> formParams = new ArrayList<NameValuePair>();

			if(map != null)
			for(String key : map.keySet()){
				String value = map.get(key);
				formParams.add(new BasicNameValuePair(key, value));
			}

			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams,sendEncoding);
			post.setEntity(formEntity);

			HttpResponse response = httpClient.execute(post);

			InputStream in = response.getEntity().getContent();

			result = IoUtil.getStringFromInputSream(in, readEncoding);

			in.close();
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			if(post!=null && !post.isAborted()){
				post.abort();
			}
		}


		return result;
	}

	 /**
     * 创建SSL安全连接
     *
     * @return
     */
	private static SSLConnectionSocketFactory createSSLConnSocketFactory() {
        SSLConnectionSocketFactory sslsf = null;
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {

                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }
        return sslsf;
    }

	/**
	 *
	 * @param url
	 * @param map
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGet(String url,Map<String,String> map) throws ClientProtocolException, IOException{
		return sendGet(url,map,DEFAULT_ENCODING,DEFAULT_ENCODING);
	}

	/**
	 *
	 * @param url
	 * @param map 请求参数
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGet(String url,Map<String,String> map,int timeout) throws ClientProtocolException, IOException{
		return sendGet(url,map,DEFAULT_ENCODING,DEFAULT_ENCODING,timeout);
	}

	/**
	 * 发送post请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @param readEncoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGet(String url,Map<String,String> map,String readEncoding) throws ClientProtocolException, IOException{
		return sendGet(url,map,readEncoding,DEFAULT_ENCODING);
	}

	/**
	 * 发送post请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @param readEncoding
	 * @param timeout
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGet(String url,Map<String,String> map,String readEncoding,int timeout) throws ClientProtocolException, IOException{
		return sendGet(url,map,readEncoding,DEFAULT_ENCODING,timeout);
	}

	/**
	 * 发送post请求
	 *
	 * @param url
	 * @param map
	 * @param sendEncoding
	 * @param readEncoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGet(String url,Map<String,String> map,String sendEncoding,String readEncoding) throws ClientProtocolException, IOException{
		return sendGet(url,map,sendEncoding,readEncoding,DEFAULT_TIMEOUT);
	}

	/**
	 * 发送post请求
	 *
	 * @param url 链接
	 * @param map 请求参数
	 * @param sendEncoding 请求字符集
	 * @param readEncoding 响应字符集
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGet(String url,Map<String,String> map,String sendEncoding,String readEncoding,int timeout) throws ClientProtocolException, IOException{

		if(url.startsWith(SCHEMER_HTTPS)) return sendGetSSL(url,map,sendEncoding,readEncoding,timeout);

		HttpClient httpClient = HttpClients.createSystem();


		HttpGet get = null;

		try{

			StringBuffer buff = new StringBuffer();

			if(map != null)
			for(String key : map.keySet()){
				String value = map.get(key);
				buff.append("&").append(key).append("=").append(URLEncoder.encode(value, sendEncoding));
			}
			if(buff.length()!=0) buff.deleteCharAt(0);


			String params = buff.toString();

			url = url.contains("?") ? (url + params) : (url + "?" + params);

			get = new HttpGet(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout)
					.setConnectTimeout(timeout).setSocketTimeout(timeout).build();
			get.setConfig(requestConfig);

			HttpResponse response = httpClient.execute(get);

			InputStream in = response.getEntity().getContent();

			String result = IoUtil.getStringFromInputSream(in, readEncoding);

			in.close();

			return result;

		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			if(get!=null && !get.isAborted()){
				get.abort();
			}
		}

	}

	/**
	 *
	 *
	 * @param url
	 * @param map
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGetSSL(String url,Map<String,String> map) throws ClientProtocolException, IOException{
		return sendGetSSL(url,map,DEFAULT_ENCODING);
	}

	/**
	 *
	 *
	 * @param url
	 * @param map
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGetSSL(String url,Map<String,String> map,int timeout) throws ClientProtocolException, IOException{
		return sendGetSSL(url,map,DEFAULT_ENCODING,timeout);
	}

	/**
	 *
	 *
	 * @param url
	 * @param map
	 * @param encoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGetSSL(String url,Map<String,String> map,String encoding) throws ClientProtocolException, IOException{
		return sendGetSSL(url,map,encoding,encoding,DEFAULT_TIMEOUT);
	}

	/**
	 *
	 *
	 * @param url
	 * @param map
	 * @param encoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGetSSL(String url,Map<String,String> map,String encoding,int timeout) throws ClientProtocolException, IOException{
		return sendGetSSL(url,map,encoding,encoding,timeout);
	}

	/**
	 *
	 *
	 *
	 * @param url
	 * @param map
	 * @param sendEncoding
	 * @param readEncoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGetSSL(String url,Map<String,String> map,String sendEncoding,String readEncoding) throws ClientProtocolException, IOException{
		return sendGetSSL(url,map,sendEncoding,readEncoding,DEFAULT_TIMEOUT);
	}

	/**
	 *
	 *
	 *
	 * @param url
	 * @param map
	 * @param sendEncoding
	 * @param readEncoding
	 * @param timeout 请求超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendGetSSL(String url,Map<String,String> map,String sendEncoding,String readEncoding,int timeout) throws ClientProtocolException, IOException{

		if(url.startsWith(SCHEMER_HTTP)) return sendGet(url,map,sendEncoding,readEncoding,timeout);

		HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).build();

		HttpGet get = null;

		try{

			StringBuffer buff = new StringBuffer();

			if(map != null)
			for(String key : map.keySet()){
				String value = map.get(key);
				buff.append("&").append(key).append("=").append(URLEncoder.encode(value, sendEncoding));
			}
			if(buff.length()!=0) buff.deleteCharAt(0);


			String params = buff.toString();

			url = url.contains("?") ? (url + params) : (url + "?" + params);

			get = new HttpGet(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout)
					.setConnectTimeout(timeout).setSocketTimeout(timeout).build();
			get.setConfig(requestConfig);

			HttpResponse response = httpClient.execute(get);

			InputStream in = response.getEntity().getContent();

			String result = IoUtil.getStringFromInputSream(in, readEncoding);

			in.close();

			return result;

		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			if(get!=null && !get.isAborted()){
				get.abort();
			}
		}

	}

	/**
	 * 发送put请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPut(String url,Map<String,String> map) throws ClientProtocolException, IOException{
		return sendPut(url,map,DEFAULT_ENCODING,DEFAULT_ENCODING);
	}

	/**
	 * 发送put请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map 请求参数
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPut(String url,Map<String,String> map,int timeout) throws ClientProtocolException, IOException{
		return sendPut(url,map,DEFAULT_ENCODING,DEFAULT_ENCODING,timeout);
	}

	/**
	 * 发送put请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPut(String url,Map<String,String> map,String readEncoding) throws ClientProtocolException, IOException{
		return sendPut(url,map,readEncoding,DEFAULT_ENCODING);
	}

	/**
	 * 发送put请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @param timeout
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPut(String url,Map<String,String> map,String readEncoding,int timeout) throws ClientProtocolException, IOException{
		return sendPut(url,map,readEncoding,DEFAULT_ENCODING,timeout);
	}

	/**
	 * 发送put请求
	 *
	 * @param url
	 * @param map
	 * @param sendEncoding
	 * @param readEncoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPut(String url,Map<String,String> map,String sendEncoding,String readEncoding) throws ClientProtocolException, IOException{
		return sendPut(url,map,sendEncoding,readEncoding,DEFAULT_TIMEOUT);
	}

	/**
	 * 发送put请求
	 *
	 * @param url 链接
	 * @param map 请求参数
	 * @param sendEncoding 请求字符集
	 * @param readEncoding 响应字符集
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPut(String url,Map<String,String> map,String sendEncoding,String readEncoding,int timeout) throws ClientProtocolException, IOException{

		if(url.startsWith(SCHEMER_HTTPS)) return sendGetSSL(url,map,sendEncoding,readEncoding,timeout);

		HttpClient httpClient = HttpClients.createDefault();

		HttpPut put = null;

		try{

			StringBuffer buff = new StringBuffer();

			if(map != null)
			for(String key : map.keySet()){
				String value = map.get(key);
				buff.append("&").append(key).append("=").append(URLEncoder.encode(value, sendEncoding));
			}
			if(buff.length()!=0) buff.deleteCharAt(0);


			String params = buff.toString();

			url = url.contains("?") ? (url + params) : (url + "?" + params);

			put = new HttpPut(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout)
					.setConnectTimeout(timeout).setSocketTimeout(timeout).build();
			put.setConfig(requestConfig);

			HttpResponse response = httpClient.execute(put);

			InputStream in = response.getEntity().getContent();

			String result = IoUtil.getStringFromInputSream(in, readEncoding);

			in.close();

			return result;

		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			if(put!=null && !put.isAborted()){
				put.abort();
			}
		}

	}

	/**
	 * http delete
	 *
	 * @param url
	 * @param map
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendDelete(String url,Map<String,String> map) throws ClientProtocolException, IOException{
		return sendDelete(url,map,DEFAULT_ENCODING,DEFAULT_ENCODING);
	}

	/**
	 * http delete
	 *
	 * @param url
	 * @param map 请求参数
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendDelete(String url,Map<String,String> map,int timeout) throws ClientProtocolException, IOException{
		return sendDelete(url,map,DEFAULT_ENCODING,DEFAULT_ENCODING,timeout);
	}

	/**
	 * 发送delete请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendDelete(String url,Map<String,String> map,String readEncoding) throws ClientProtocolException, IOException{
		return sendDelete(url,map,readEncoding,DEFAULT_ENCODING);
	}

	/**
	 * 发送delete请求，默认发送字符编码UTF-8
	 *
	 * @param url
	 * @param map
	 * @param timeout
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendDelete(String url,Map<String,String> map,String readEncoding,int timeout) throws ClientProtocolException, IOException{
		return sendDelete(url,map,readEncoding,DEFAULT_ENCODING,timeout);
	}

	/**
	 * 发送delete请求
	 *
	 * @param url
	 * @param map
	 * @param sendEncoding
	 * @param readEncoding
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendDelete(String url,Map<String,String> map,String sendEncoding,String readEncoding) throws ClientProtocolException, IOException{
		return sendDelete(url,map,sendEncoding,readEncoding,DEFAULT_TIMEOUT);
	}

	/**
	 * 发送delete请求
	 *
	 * @param url 链接
	 * @param map 请求参数
	 * @param sendEncoding 请求字符集
	 * @param readEncoding 响应字符集
	 * @param timeout 超时时间，毫秒
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendDelete(String url,Map<String,String> map,String sendEncoding,String readEncoding,int timeout) throws ClientProtocolException, IOException{

		if(url.startsWith(SCHEMER_HTTPS)) return sendGetSSL(url,map,sendEncoding,readEncoding,timeout);

		HttpClient httpClient = HttpClients.createDefault();

		HttpDelete delete = null;

		try{

			StringBuffer buff = new StringBuffer();

			if(map != null)
			for(String key : map.keySet()){
				String value = map.get(key);
				buff.append("&").append(key).append("=").append(URLEncoder.encode(value, sendEncoding));
			}
			if(buff.length()!=0) buff.deleteCharAt(0);


			String params = buff.toString();

			url = url.contains("?") ? (url + params) : (url + "?" + params);

			delete = new HttpDelete(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout)
					.setConnectTimeout(timeout).setSocketTimeout(timeout).build();
			delete.setConfig(requestConfig);

			HttpResponse response = httpClient.execute(delete);

			InputStream in = response.getEntity().getContent();

			String result = IoUtil.getStringFromInputSream(in, readEncoding);

			in.close();

			return result;

		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			if(delete!=null && !delete.isAborted()){
				delete.abort();
			}
		}

	}

	public static void println(Header[] allHeaders) {
		if(allHeaders != null){
			for(Header header: allHeaders){
				System.out.println(header.getName()+":"+header.getValue());
			}
		}
	}

	/**
	 *
	 * @param url
	 * @param data
	 * @return
	 */
	public static String sendPostData(String url,String data){
		return sendPostData(url,data,DEFAULT_ENCODING);
	}

	/**
	 *
	 * @param url
	 * @param data
	 * @param charset
	 * @return
	 */
	public static String sendPostData(String url,String data,String charset){

		String result = null;

		HttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.setHeader(HTTP.CONTENT_TYPE, "text/html;charset="+charset);

		try{
			httpPost.setEntity(new StringEntity(data, charset));
			HttpResponse response = httpClient.execute(httpPost);
			InputStream in = response.getEntity().getContent();
			result = IoUtil.getStringFromInputSream(in, charset);

			in.close();
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			if(httpPost!=null && !httpPost.isAborted()){
				httpPost.abort();
			}
		}
		return result;
	}

	/**
	 *
	 * @param url
	 * @param data
	 * @return
	 */
	public static String sendPostData(String url,byte[] data){
		return sendPostData(url,data,DEFAULT_ENCODING);
	}

	/**
	 *
	 * @param url
	 * @param data
	 * @param charset
	 * @return
	 */
	public static String sendPostData(String url,byte[] data,String charset){

		String result = null;

		HttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.setHeader(HTTP.CONTENT_TYPE, "text/html;charset="+charset);

		try{
			httpPost.setEntity(new ByteArrayEntity(data));
			HttpResponse response = httpClient.execute(httpPost);
			InputStream in = response.getEntity().getContent();
			result = IoUtil.getStringFromInputSream(in, charset);

			in.close();
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			if(httpPost!=null && !httpPost.isAborted()){
				httpPost.abort();
			}
		}
		return result;
	}

}
